﻿
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;

using System.Windows.Input;
using System.Windows.Threading;
using System.Runtime.Serialization;
using System.Security.Permissions;



namespace ApplicationBoom.Command
{

    [Serializable()]
    public class RelayCommand : ICommand, ISerializable
    {
        #region "Fields"

        /// <summary>
        /// The m_ execute
        /// </summary>
        [NonSerialized()]
        readonly Action<object> m_Execute;
        /// <summary>
        /// The m_ can execute
        /// </summary>
        [NonSerialized()]

        readonly Predicate<object> m_CanExecute;
        #endregion

        #region "Constructors"

        /// <summary>
        /// Creates a new command that can always execute.
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        public RelayCommand(Action<object> execute)
            : this(execute, null)
        {
            CommandCanExecute = true;
        }

        /// <summary>
        /// Creates a new command.
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        /// <param name="canExecute">The execution status logic.</param>
        /// <exception cref="System.ArgumentNullException">execute</exception>
        public RelayCommand(Action<object> execute, Predicate<object> canExecute)
        {
            if (execute == null)
            {
                throw new ArgumentNullException("execute");
            }

            m_Execute = execute;
            m_CanExecute = canExecute;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="RelayCommand"/> class.
        /// </summary>

        protected RelayCommand()
        {
        }

        #endregion

        #region "ICommand Members"

        /// <summary>
        /// Defines the method that determines whether the command can execute in its current state.
        /// </summary>
        /// <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
        /// <returns>true if this command can be executed; otherwise, false.</returns>
        [DebuggerStepThrough()]
        public bool CanExecute(object parameter)
        {
            return m_CanExecute == null ? CommandCanExecute : m_CanExecute(parameter);
        }

        /// <summary>
        /// Occurs when [can execute changed].	
        /// </summary>
        /// <remarks></remarks>
        public event EventHandler CanExecuteChanged;
        /// <summary>
        /// Called when [can execute changed].
        /// </summary>
        public void OnCanExecuteChanged()
        {
            if (CanExecuteChanged != null)
            {
                CanExecuteChanged(this, EventArgs.Empty);
            }

        }

        /// <summary>
        /// Defines the method to be called when the command is invoked.
        /// </summary>
        /// <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
        public void Execute(object parameter)
        {
            m_Execute(parameter);
        }

        #endregion

        /// <summary>
        /// Gets or sets a value indicating whether [command can execute].
        /// </summary>
        /// <value><c>true</c> if [command can execute]; otherwise, <c>false</c>.</value>
        public bool CommandCanExecute
        {
            get { return m_CommandCanExecute; }
            set
            {
                if (m_CommandCanExecute == value)
                {
                    return;
                }
                m_CommandCanExecute = value;
                if (CanExecuteChanged != null)
                {
                    CanExecuteChanged(this, EventArgs.Empty);
                }
            }
        }
        /// <summary>
        /// The m_ command can execute
        /// </summary>

        private bool m_CommandCanExecute;


        #region "Deserialization Constructor"


        protected RelayCommand(SerializationInfo info, StreamingContext context)
        {
            m_CommandCanExecute = Convert.ToBoolean(info.GetValue("m_CommandCanExecute", typeof(bool)));

        }

        #endregion

        #region "ISerializable Implementation"

        [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]

        public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("m_CommandCanExecute", m_CommandCanExecute);

        }

        #endregion
    }

}
